<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include ('../global/initialize.php');

aos_header('Letter');

$query = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND invited = 0 AND requested = 0";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if (isset ($row['faction_id'])) {
	$faction_id_tmp = $row['faction_id'];
} else {
	$faction_id_tmp = -1;
}

if (isset ($_GET['id'])) {
	$message_id = formatInput($_GET['id']);
} else {
	$message_id = -1;
}

//print( "message_id: $message_id <br />" );

if (is_numeric($message_id)) {
	$query = "SELECT sender, parent FROM messages WHERE message_id = " . $message_id;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$parent = $row['parent'];

	$allow = false;
	if ($row['sender'] == $_SESSION['ruler_id']) {
		$allow = true;
	} else {
		$query2 = "SELECT ruler_id " .
		"FROM message_ruler " .
		"WHERE message_id = $message_id AND ruler_id = " . $_SESSION['ruler_id'];
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
		if ($row2['ruler_id'] == $_SESSION['ruler_id']) {
			$allow = true;
		}
	}
	if ($allow) {
		$query = "UPDATE message_ruler SET seen = 1 WHERE message_id = " . $message_id . " AND ruler_id = " . $_SESSION['ruler_id'];
		mysql_query($query) or die("Query failed : " . mysql_error());
		$query = "SELECT name, ruler.ruler_id FROM ruler JOIN message_ruler ON ruler.ruler_id = message_ruler.ruler_id WHERE message_id = " . $message_id;
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$num = mysql_numrows($result);

		$to = ""; //mysql_result( $result, 0, "name" );
		$i = 0;
		while ($i < $num) {
			$query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result($result, $i, "ruler_id") . " AND invited = 0 AND requested = 0";
			$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
			$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
			if ($_SESSION['ruler_id'] == mysql_result($result, $i, "ruler_id")) {
				$strong = "<strong class=\"green\">";
			} else
				if (!isset ($row3['faction_id']) || $faction_id_tmp != $row3['faction_id']) {
					$strong = "<strong class=\"red\">";
				} else {
					$strong = "<strong class=\"blue\">";
				}

			if ($i == 0) {
				$to = "<a href=\"../overview/ruler.php?name=" . mysql_result($result, $i, "name") . "\">$strong" . mysql_result($result, $i, "name") . "</strong></a>";
			} else {
				$to = $to . ", <a href=\"../overview/ruler.php?name=" . mysql_result($result, $i, "name") . "\">$strong" . mysql_result($result, $i, "name") . "</strong></a>";
				//$to = $to . ", " . mysql_result( $result, $i, "name" );
			}
			$i++;
		}

		$query = "SELECT * FROM messages WHERE message_id = " . $message_id;
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$row = mysql_fetch_array($result, MYSQL_ASSOC);
		$query2 = "SELECT name, ruler_id FROM ruler WHERE ruler_id = " . $row['sender'];
		$result2 = mysql_query($query2) or die("Query failed : " . mysql_error());
		$row2 = mysql_fetch_array($result2, MYSQL_ASSOC);

		print "<div class=\"blok\">";
		print "<strong>" . translate("Diplomacy") . ":</strong><br />";
		printDiplomacyTab();
		print "</div><br />\r\n";

		if ($parent != -1) {
			$query3 = "SELECT sender FROM messages WHERE message_id = " . $parent;
			$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
			$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
			$allow = false;
			if ($row3['sender'] == $_SESSION['ruler_id']) {
				$allow = true;
			} else {
				$query3 = "SELECT ruler_id " .
				"FROM message_ruler " .
				"WHERE message_id = $parent AND ruler_id = " . $_SESSION['ruler_id'];
				$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				if ($row3['ruler_id'] == $_SESSION['ruler_id']) {
					$allow = true;
				}
			}
			if ($allow) {
				$query = "UPDATE message_ruler SET seen = 1 WHERE message_id = " . $parent . " AND ruler_id = " . $_SESSION['ruler_id'];
				mysql_query($query) or die("Query failed : " . mysql_error());

				$query3 = "SELECT subject, date, sender, text, message_id FROM messages WHERE message_id = " . $parent;
				$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				$parent_div = true;
				$count = getThreadCount($row3['message_id']);
				if ($count > 1) {
					$count = " ($count)";
				} else {
					$count = "";
				}
				//print( "<div class=\"message-parent\">\r\n" );
				print ("<div class=\"message\">\r\n");
				print ("<a href=\"letter.php?id=" . $parent . "#focus\"><strong>" . translate("Subject") . ": " . stripslashes($row3['subject']) . $count . "</strong></a><br />\r\n");
				print ("<small>" . translate("Date") . ": " . $row3['date'] . ", " . translate("From") . ": ");
				$query4 = "SELECT name FROM ruler WHERE ruler_id = " . $row3['sender'];
				$result4 = mysql_query($query4) or die("Query failed : " . mysql_error());
				$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
				print ($row4['name'] . "</small><br />");
				print ("<pre>" . wordwrap(stripslashes($row3['text']), 80, "\n") . "</pre>");
			}
		}

		if (isset ($_SESSION['send_copy_status'])) {
			print translate("Status") . ": " . $_SESSION['send_copy_status'] . "<br/><br/>\r\n";
			unset ($_SESSION['send_copy_status']);
		}

		print ("<div class=\"main-message\"><a name=\"focus\"></a>\r\n");
		$count = getThreadCount($row['message_id']);
		if ($count > 1) {
			$count = " ($count)";
		} else {
			$count = "";
		}

		print "<strong>" . translate("Subject") . ": " . stripslashes($row['subject']) . $count . "</strong><br/>\r\n";
		if ($row['sender'] != $_SESSION['ruler_id']) {
			$query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $row['sender'] . " AND invited = 0 AND requested = 0";
			$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
			$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
			if (!isset ($row3['faction_id']) || $faction_id_tmp != $row3['faction_id']) {
				$strong = "<strong class=\"red\">";
			} else {
				$strong = "<strong class=\"blue\">";
			}
			print translate("From") . ": <a href=\"../overview/ruler.php?name=" . $row2['name'] . "\">$strong" . $row2['name'] . "</strong></a> \r\n";
			print "<form method=\"post\" action=\"send_letter.php\">\r\n";
			print "<input type=\"hidden\" value=\"$message_id\" name=\"message_id\" />\r\n" .
			"<input type=\"submit\" value=\"" . translate("Send an answer") . "\" /></form><br />\r\n";
		}
		print "<form method=\"post\" action=\"send_copy.php\">\r\n";
		print translate("To") . ": " . $to . "<br />" . translate("Send a copy to") . ": <input type=\"text\" name=\"to\" />\r\n" .
		"<input type=\"hidden\" value=\"$message_id\" name=\"message_id\" />\r\n" .
		"<input type=\"submit\" value=\"" . translate("send") . "\" /></form><br />\r\n";
		print translate("Date") . ": <small>" . $row['date'] . "</small><br />\r\n";
		print "<pre>" . wordwrap(stripslashes($row['text']), 80, "\n") . "</pre>\r\n";

		$query = "SELECT subject, message_id, date, sender, text " .
		"FROM messages " .
		"WHERE parent = " . $message_id .
		" ORDER BY turn DESC, timestamp DESC";
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$num = mysql_numrows($result);
		$i = 0;
		while ($i < $num) {
			$allow = false;
			if (mysql_result($result, $i, "sender") == $_SESSION['ruler_id']) {
				$allow = true;
			} else {
				$query3 = "SELECT ruler_id " .
				"FROM message_ruler " .
				"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND message_id = " . mysql_result($result, $i, "message_id");
				$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				if ($row3['ruler_id'] == $_SESSION['ruler_id']) {
					$allow = true;
				}
			}
			if ($allow) {
				$query = "UPDATE message_ruler SET seen = 1 WHERE message_id = " . mysql_result($result, $i, "message_id") . " AND ruler_id = " . $_SESSION['ruler_id'];
				mysql_query($query) or die("Query failed : " . mysql_error());
				if ($i == 0) {
					if ($num == 1) {
						print (translate("Answer") . ":<br />\r\n");
					} else {
						print (translate("Answers") . ":<br />\r\n");
					}
				} else {
					print ("<br />\r\n");
				}
				print ("<div class=\"message-child\">\r\n");

				$count = getThreadCount(mysql_result($result, $i, "message_id"));
				if ($count > 1) {
					$count = " ($count)";
				} else {
					$count = "";
				}

				print ("<a href=\"letter.php?id=" . mysql_result($result, $i, "message_id") . "#focus\"><strong>" . translate("Subject") . ": " . stripslashes(mysql_result($result, $i, "subject")) . $count . "</strong></a><br />\r\n");
				$query3 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result($result, $i, "sender");
				$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
				$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
				print ("<small>" . translate("Date") . ": " . mysql_result($result, $i, "date") . ", " . translate("From") . ": " . $row3['name'] . "</small><br />");
				print ("<pre>" . wordwrap(stripslashes(mysql_result($result, $i, "text")), 80, "\n") . "</pre>\r\n");

				$query4 = "SELECT subject, message_id, date, sender " .
				"FROM messages " .
				"WHERE parent = " . mysql_result($result, $i, "message_id") .
				" ORDER BY turn DESC, timestamp DESC";
				$result4 = mysql_query($query4) or die("Query failed : " . mysql_error());
				$num4 = mysql_numrows($result4);
				$i4 = 0;
				while ($i4 < $num4) {
					$allow4 = false;
					if (mysql_result($result4, $i4, "sender") == $_SESSION['ruler_id']) {
						$allow4 = true;
					} else {
						$query3 = "SELECT count(ruler_id) FROM message_ruler WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND message_id = " . mysql_result($result4, $i4, "message_id");
						$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
						$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
						if ($row3['count(ruler_id)'] > 0) {
							$allow4 = true;
						}
					}
					if ($allow4) {
						if ($i4 > 0) {
							print ("<br />\r\n");
						}
						print ("<div class=\"message-grand-child-preview\">\r\n");

						$count = getThreadCount(mysql_result($result4, $i4, "message_id"));
						if ($count > 1) {
							$count = " ($count)";
						} else {
							$count = "";
						}

						print ("<a href=\"letter.php?id=" . mysql_result($result4, $i4, "message_id") . "#focus\">" . translate("Subject") . ": " . stripslashes(mysql_result($result, $i, "subject")) . $count . "</a>\r\n");
						$query3 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result($result4, $i4, "sender");
						$result3 = mysql_query($query3) or die("Query failed : " . mysql_error());
						$row3 = mysql_fetch_array($result3, MYSQL_ASSOC);
						print (" - <small>" . translate("Date") . ": " . mysql_result($result4, $i4, "date") . ", " . translate("From") . ": " . $row3['name'] . "</small>\r\n");
						print ("</div> <!-- close message-grand-child-preview -->\r\n");
					}
					$i4++;
				}
				print ("</div> <!-- close message-child -->\r\n");
			}
			$i++;
		}
		$query = "SELECT messages.message_id " .
		"FROM messages " .
		"JOIN message_ruler ON message_ruler.message_id = messages.message_id " .
		"WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND hide = 0 " .
		"ORDER BY turn DESC, timestamp DESC";
		$result = mysql_query($query) or die("Query failed : " . mysql_error());
		$num = mysql_numrows($result);
		$i = 0;
		$set_br = false;
		while ($i < $num) {
			if (mysql_result($result, $i, "message_id") == $message_id) {
				if ($i > 0) {
					if (!$set_br) {
						print ("<br />\r\n");
						$set_br = true;
					}
					print ("<div id=\"text_right\"><a href=\"letter.php?id=" . mysql_result($result, ($i -1), "message_id") . "#focus\">" . translate("Next") . "</a></div>\r\n");
				}
				if ($i < $num -1) {
					if (!$set_br) {
						print ("<br />\r\n");
						$set_br = true;
					}
					print ("<div id=\"text_left\"><a href=\"letter.php?id=" . mysql_result($result, ($i +1), "message_id") . "#focus\">" . translate("Previous") . "</a></div><br />\r\n");
				} else {
					print ("<br />\r\n");
				}
			}
			$i++;
		}
		print "</div>\r\n";
		if (isset ($parent_div)) {
			print ("</div> <!-- close message-parent -->\r\n");
		}
	} else {
		print "<div id=\"main-content\">";
		print "<div class=\"blok\">";
		print "<strong>" . translate("Diplomacy") . ":</strong><br/>";
		printDiplomacyTab();
		print "</div><br/>\r\n";
		print "<div class=\"blok\">\r\n";
		print translate("Selected letter does not exist.");
	}
} else {
	print "<div id=\"main-content\">";
	print "<div class=\"blok\">";
	print "<strong>" . translate("Diplomacy") . ":</strong><br/>";
	printDiplomacyTab();
	print "</div><br/>\r\n";
	print "<div class=\"blok\">\r\n";
	print translate("No letter selected.");
}
aos_footer();
?>
